//
//  WorkServicesViewController.m
//  ASHT_IOS
//
//  Created by 宋安安 on 14-4-5.
//  Copyright (c) 2014年 宋安安. All rights reserved.
//

#import "WorkServicesViewController.h"
#import "WorkServicesCell.h"
#import "UITabBarController+makeTabBarHidden.h"
#import "AFAppDotNetAPIClient.h"
#import "WorkServiceEntity.h"
#import "WorkServicesSubViewController.h"


@interface WorkServicesViewController ()

@end

@implementation WorkServicesViewController
@synthesize workServiceArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        isarler=YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    workServiceArray = [[NSMutableArray alloc]init];
    CGRect rect;
//    [self.tabBarController makeTabBarHidden:NO];
    //获取当前屏幕
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    if(screenRect.size.height != 480)
    {
        rect=CGRectMake(10, 0,300,self.view_Content.frame.size.height+66);
    }
    else
    {
        rect=CGRectMake(10, 0,300,self.view_Content.frame.size.height-20);
    }
    gridview=[[UIGridView alloc]initWithFrame:rect];
    gridview.autoresizingMask=UIViewAutoresizingNone;
    [self.view_Content addSubview:gridview];

    [gridview setBackgroundColor:[UIColor clearColor]];
//    girdview.transform=CGAffineTransformMakeRotation(degressToRadians(-90));
//    gridview.pagingEnabled=YES;
    gridview.frame=rect;
    gridview.showsVerticalScrollIndicator = NO;
    gridview.uiGridViewDelegate=self;
//    gridview.dataSource=self;
//    [gridview setUp];
    gridview.backgroundColor=[UIColor clearColor];
//    self.view_Content.backgroundColor=[UIColor blackColor];
    //获取办事服务分类
    [self getWorkServiceData];
    

}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    isarler=YES;
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat) gridView:(UIGridView *)grid widthForColumnAt:(int)columnIndex
{
	return 100;
}

- (CGFloat) gridView:(UIGridView *)grid heightForRowAt:(int)rowIndex
{
	return 100;
}

- (NSInteger) numberOfColumnsOfGridView:(UIGridView *) grid
{
	return 3;
}


- (NSInteger) numberOfCellsOfGridView:(UIGridView *) grid
{
    return workServiceArray.count;
}

- (UIGridViewCell *) gridView:(UIGridView *)grid cellForRowAt:(int)rowIndex AndColumnAt:(int)columnIndex
{
    static NSString *CellIdentifier = @"WorkServicesIdentifier";
    
    WorkServicesCell *cell = [gridview dequeueReusableCellWithIdentifier:CellIdentifier];

	if (cell == nil) {
        NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"WorkServicesCell" owner:self options:nil];
        cell = [nibViews objectAtIndex:0];
        nibViews=nil;

	}

    int workIndex = rowIndex *3 + columnIndex;
    WorkServiceEntity *curEntity = [self.workServiceArray objectAtIndex:workIndex];
    cell.lab_label1.text= curEntity.categoryName;

    [cell imageUrl:curEntity.imageUrl];

	return cell;
}

- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)colIndex
{
    int index=3*rowIndex+colIndex;
    WorkServiceEntity *curEntity = [self.workServiceArray objectAtIndex:index];
    NSString *parentId = curEntity.categoryId;
    NSString *url = [NSString stringWithFormat:@"/phone!getThreeCategoryList.action?parentId=%@",parentId];
    NSMutableArray *subDataArray = [NSMutableArray array];
    [[AFAppDotNetAPIClient sharedClient] postPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSDictionary *dic_result=[NSDictionary dictionaryWithDictionary:JSON];
        NSArray *arr_success=[dic_result objectForKey:@"context"];
        
        for (NSDictionary * item in arr_success) {
            WorkServiceEntity *entity = [[WorkServiceEntity alloc] init];

            entity.categoryId = [item objectForKey:@"categoryId"];
            entity.categoryName = [item objectForKey:@"categoryName"];
            entity.imageUrl = [item objectForKey:@"imageUrl"];
            [subDataArray addObject:entity];
            entity = nil;
        }
        subView = [[WorkServicesSubViewController alloc] initWithNibName:XIB_Version(@"WorkServicesSubViewController") bundle:nil];
        subView.subArray = subDataArray;
        subView.headName = curEntity.categoryName;
        subView.imgUrl = curEntity.imageUrl;
        [self.app.viewDeckController.navigationController pushViewController:subView animated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        if (isarler) {
            NSString *message=[self ErrorMessageHandling:error];
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示"
                                                          message:message
                                                         delegate:nil
                                                cancelButtonTitle:@"确定"
                                                otherButtonTitles:nil];
            [alert show];
            alert=nil;
            isarler=NO;
        }


        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];

    
}



-(void) getWorkServiceData
{
    NSString *url=@"phone!getBsfwCategoryList.action";
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[AFAppDotNetAPIClient sharedClient] postPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
        NSDictionary *dic_result=[NSDictionary dictionaryWithDictionary:JSON];
        NSArray *arr_success=[dic_result objectForKey:@"context"];
        
        
        for (NSDictionary * item in arr_success) {
            WorkServiceEntity *entity = [[WorkServiceEntity alloc] init];
            entity.categoryFlag = [item objectForKey:@"categoryFlag"];
            entity.categoryId = [item objectForKey:@"categoryId"];
            entity.categoryName = [item objectForKey:@"categoryName"];
            entity.imageUrl = [item objectForKey:@"imageUrl"];
            [self.workServiceArray addObject:entity];
            entity = nil;
            
        }

        [gridview reloadData];
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        if (isarler) {
            NSString *message=[self ErrorMessageHandling:error];
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示"
                                                          message:message
                                                         delegate:nil
                                                cancelButtonTitle:@"确定"
                                                otherButtonTitles:nil];
            [alert show];
            alert=nil;
            isarler=NO;
        }
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];
}



@end
